package com.thy.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.base.PageResult;
import com.hospital.common.base.Result;
import com.hospital.common.enums.RoleType;
import com.hospital.common.exception.GlobalException;
import com.hospital.common.pojo.TDepartment;
import com.hospital.common.pojo.TDoctorInfo;
import com.hospital.common.pojo.TUser;
import com.hospital.common.vo.management.module.DepartmentAndStatVo;
import com.hospital.common.vo.management.module.DepartmentVo;
import com.hospital.common.vo.management.module.DoctorInfoVo;
import com.thy.feign.DataSimulationFeign;
import com.thy.mapper.TDepartmentMapper;
import com.thy.service.TDepartmentService;
import com.thy.service.TDoctorInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 科室表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-09-11
 */
@Service
public class TDepartmentServiceImpl extends ServiceImpl<TDepartmentMapper, TDepartment> implements TDepartmentService {
    @Autowired
    private TDepartmentMapper tDepartmentMapper;
    @Autowired
    private TUserServiceImpl tUserService;
    @Autowired
    private TDoctorInfoService tDoctorInfoService;
    @Autowired
    private DataSimulationFeign dataSimulationFeign;

    @Override
    public List<DepartmentVo> getAllDepartment() {
       return tDepartmentMapper.getAllDepartment();
    }

    @Override
    public List<DoctorInfoVo> getDoctorByDepartmentId(Long departmentId) {
        return tDepartmentMapper.getDoctorByDepartmentId(departmentId);
    }

    @Override
    public void addDepartment(TDepartment department) {
        Result<Long> longResult = dataSimulationFeign.addDepartment(department);
        if(longResult.getCode() == 0){
            throw new GlobalException(20002,"添加科室失败");
        }
    }

    @Override
    public PageResult<DoctorInfoVo> getDoctorByDepartmentIds(List<Long> departmentIds, Integer pageNum, Integer pageSize) {
        Page<DoctorInfoVo> page = new Page<>(pageNum,pageSize);
        IPage<DoctorInfoVo> doctorInfoVos = tDepartmentMapper.getDoctorByDepartmentIds(departmentIds,page);
        if (doctorInfoVos != null){
            return new PageResult<>(doctorInfoVos.getTotal(), doctorInfoVos.getRecords());
        }else {
            return new PageResult<>(0L, new ArrayList<>());
        }
    }

    @Override
    public List<DepartmentAndStatVo> getDepartmentAndStat() {
        return tDepartmentMapper.selectList(null).stream().map(this::convert).toList();
    }
    private DepartmentAndStatVo convert(TDepartment department){
        DepartmentAndStatVo departmentAndStatVo = new DepartmentAndStatVo();
        LambdaQueryWrapper<TUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TUser::getInfoId)
                .eq(TUser::getRole, RoleType.DOCTOR.value);
        List<TUser> users = tUserService.list(queryWrapper);
        LambdaQueryWrapper<TDoctorInfo> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(TDoctorInfo::getDepartmentId,department.getId())
                        .in(TDoctorInfo::getId,users.stream().map(TUser::getInfoId).toList())
                        .eq(TDoctorInfo::getDeleted,0);
        BeanUtils.copyProperties(department,departmentAndStatVo);
        departmentAndStatVo.setDoctorCount(tDoctorInfoService.count(queryWrapper1));
        return departmentAndStatVo;
    }
}
